package com.zhny.aiot.gd.sfj.plugin.model;

import com.zhny.aiot.plugin.modbus.device.ModemDevice;
import com.zhny.aiot.plugin.modbus.handler.model.ModemResponseMessage;
import com.zhny.aiot.plugin.model.MessageEntry;
import com.zhny.aiot.plugin.model.MessageType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DecimalFormat;

public class FerilizerTableResultModel extends FertilizerResultModel {
    private final Logger logger = LoggerFactory.getLogger(FerilizerTableResultModel.class);
    public FerilizerTableResultModel(ModemResponseMessage modemBusResponse, ModemDevice device) {
        super (modemBusResponse,device);
    }

    @Override
    protected void onDecoder(MessageEntry entry) {
        logger.info("---fertilizer writer table message---");
        int len = getModemBusResponse ().getSize ();
        if(len == 12){
            FertilizerStoreEntry storeEntry = new FertilizerStoreEntry ();
            DecimalFormat decimalFormat = new DecimalFormat("#.00");
            storeEntry.setPh (Float.parseFloat (decimalFormat.format (getModemBusResponse ().getData ().readFloat ())));
            storeEntry.setEc (Float.parseFloat (decimalFormat.format (getModemBusResponse ().getData ().readFloat ())));
            storeEntry.setPressure (Float.parseFloat (decimalFormat.format (getModemBusResponse ().getData ().readFloat ())));
            entry.setData (storeEntry);
            getDevice ().saveReal (storeEntry);
            getDevice ().save (storeEntry);
        }
        else{
            entry.setType (MessageType.ERROR);
            entry.setData ("response length error !");
        }
        getDevice ().nofity (entry);
    }

}
